<template>
  <div class="login-container">
    <!-- 顶部标题栏 -->
    <van-nav-bar title="账号登录" class="login-title" right-arrow>
      <template #right>
        <router-link to="/login-help" class="login-help">登录遇到问题</router-link>
      </template>
    </van-nav-bar>
    
    <!-- 应用图标和说明 -->
    <div class="app-info">
      <div class="app-logo">
        <img src="/src/assets/logo.png" alt="应用logo" class="logo-image">
      </div>
      <p class="app-desc">用户名密码登录 安全便捷</p>
    </div>
    
    <!-- 登录表单 -->
    <van-form @submit="handleLogin" class="login-form">
      <!-- 成功提示 -->
      <div v-if="showSuccess" class="message-success">
        <van-icon name="success" class="success-icon" />
        <span>登录成功，正在跳转...</span>
      </div>
      
      <!-- 表单错误提示 -->
      <div v-if="formError" class="form-error">
        <van-icon name="warning" class="error-icon" />
        <span>{{ formError }}</span>
      </div>
      
      <van-cell-group inset class="form-group">
        <van-field
          v-model="loginForm.username"
          name="username"
          label="用户名"
          placeholder="请输入用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
          class="form-field"
        />
        
        <van-field
          v-model="loginForm.password"
          type="password"
          name="password"
          label="密码"
          placeholder="请输入密码"
          :rules="[
            { required: true, message: '请填写密码' },
            { pattern: /^[A-Za-z0-9]{6,16}$/, message: '密码至少需要6位且只能包含字母和数字' }
          ]"
          class="form-field"
        />
      </van-cell-group>
      
      <!-- 同意条款 -->
      <div class="agreement">
        <van-checkbox v-model="agreeTerms" class="agreement-checkbox" />
        <label class="agreement-text">同意用户协议、隐私政策和服务条款</label>
      </div>
      
      <!-- 登录按钮（添加加载状态） -->
      <van-button 
        type="primary" 
        round 
        block 
        native-type="submit" 
        class="login-btn"
        :disabled="!agreeTerms || isLoading"
        :loading="isLoading"
      >
        登录
      </van-button>
      
      <!-- 其他登录选项 -->
      <div class="other-options">
        <router-link to="/forgot-password" class="forgot-password">忘记密码?</router-link>
        <router-link to="/register" class="register-btn">注册账号</router-link>
      </div>
    </van-form>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user';

const userStore = useUserStore();
const router = useRouter();

const loginForm = ref({
  username: '',
  password: ''
});
const agreeTerms = ref(false);
const isLoading = ref(false);
const formError = ref('');
const showSuccess = ref(false); // 新增：显示成功状态

const handleLogin = async () => {
  formError.value = '';
  showSuccess.value = false;
  
  if (!loginForm.value.username || !loginForm.value.password) {
    formError.value = '请填写用户名和密码';
    return;
  }
  
  if (!agreeTerms.value) {
    formError.value = '请同意用户协议和隐私政策';
    return;
  }
  
  try {
    isLoading.value = true;
    await userStore.login(loginForm.value);
    
    // 显示成功提示
    showSuccess.value = true;
    
    // 延迟跳转，让用户看到成功提示
    setTimeout(() => {
      router.push('/personal');
    }, 1500);
    
  } catch (e) {
    console.log('登录错误：', e);
    formError.value = '登录失败：' + (e.message || '用户名或密码错误');
  } finally {
    isLoading.value = false;
  }
};
</script>

<style lang="scss" scoped>
.login-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  box-sizing: border-box;
  min-height: 100vh;
  background-color: #f7f7f7;
}

.login-title {
  background-color: #fff;
  .van-nav-bar__right {
    color: #333;
    font-size: 14px;
  }
}

.app-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0 10px;
}

.app-logo {
  margin-bottom: 10px;
}

.logo-image {
  width: 60px;
  height: 60px;
  object-fit: contain;
  border-radius: 12px;
}

.app-desc {
  color: #666;
  font-size: 14px;
  margin-bottom: 10px;
}

.login-form {
  padding: 0;
}

.form-group {
  background-color: transparent;
  margin-bottom: 15px;
}

.form-field {
  background-color: #fff;
  margin-bottom: 10px;
  border-radius: 8px;
}

.agreement {
  display: flex;
  align-items: center;
  padding: 0 10px 15px;
  font-size: 12px;

  .agreement-checkbox {
    margin-right: 5px;
    --van-checkbox-size: 14px;
    --van-checkbox-color: #ff9a00;
  }

  .agreement-text {
    color: #999;
    line-height: 1.4;
  }
}

.login-btn {
  background-color: #ff9a00;
  border-color: #ff9a00;
  height: 45px;
  font-size: 16px;
  margin-top: 5px;
  --van-button-primary-background: #ff9a00;
  --van-button-primary-border-color: #ff9a00;
  --van-button-round-border-radius: 22px;
}

.other-options {
  display: flex;
  justify-content: space-between;
  padding: 0 10px 20px;
  font-size: 14px;

  .forgot-password {
    color: #666;
    text-decoration: none;
  }

  .register-btn {
    color: #ff9a00;
    text-decoration: none;
    font-weight: 500;
  }
}

// 错误提示样式
.form-error {
  display: flex;
  align-items: center;
  color: #f53f3f;
  font-size: 14px;
  padding: 10px;
  margin-bottom: 10px;
  background-color: #fff2f0;
  border-radius: 6px;
  border-left: 3px solid #f53f3f;
  
  .error-icon {
    margin-right: 8px;
    font-size: 16px;
  }
}

// 成功提示样式
.message-success {
  display: flex;
  align-items: center;
  color: #07c160;
  font-size: 14px;
  padding: 10px;
  margin-bottom: 10px;
  background-color: #f0fff8;
  border-radius: 6px;
  border-left: 3px solid #07c160;
  
  .success-icon {
    margin-right: 8px;
    font-size: 16px;
  }
}
</style>